/**
 * -- Url 模块 --
 * 用于处理Url中参数
 */

if (typeof (window.location) == 'undefined') {
	console.error('[web-tool url model] window.location is undefined')
}

function getvars(): object {
	var vars = {},
		hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for (var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		if (typeof (hash[1]) != 'undefined') vars[hash[0]] = hash[1];
	}
	return vars;
}

function getHashVars(): object {
	var vars = {};
	window.location.href.replace(/[?&]+([^=&]+)=([^&#]*)/gi, (_full: string, key: string, value: string): any => vars[key] = value);
	return vars;
}

function getVar(key?): null | object | string {
	let vars = getvars();
	if (typeof (key) == 'undefined') {
		return vars
	} else {
		return vars[key] ? vars[key] : null;
	}
}

function getHashVar(key?): null | object | string {
	let vars = getHashVars();
	if (typeof (key) == 'undefined') {
		return vars;
	} else {
		return vars[key] ? vars[key] : null;
	}
}

function getHashPath(): string {
	return window.location.origin + window.location.hash.split('?')[0];
}

export {
	getVar,
	getHashVar,
	getHashPath
}
